package com.itc.bpm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itc.bpm.bean.BpmFormDesign;
import com.itc.bpm.bean.BpmModel;
import com.itc.bpm.service.BpmFormDesignService;
import com.itc.common.core.domain.R;
import com.itc.common.core.utils.StringUtils;
import com.itc.common.core.validate.Add;
import com.itc.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "表单设计管理接口类")
@RequestMapping("/bpm/form")
public class BpmFormDesignController {
    @Autowired
    BpmFormDesignService bpmFormDesignService;

    @PostMapping("/page")
    @ApiOperation(value = "查询表单设计分页")
    public R<Page<BpmFormDesign>> getPage(@RequestBody BpmFormDesign bpmFormDesign)
    {
        Page<BpmFormDesign> page = new Page<>();
        page.setOptimizeCountSql(true);
        page.setSize(bpmFormDesign.getSize());
        page.setCurrent(bpmFormDesign.getCurrent());
        bpmFormDesign.setDelFlag(0);
        bpmFormDesign.setStatus(0);
        LambdaQueryWrapper<BpmFormDesign> queryWrapper = new LambdaQueryWrapper<>(bpmFormDesign).orderByDesc(BpmFormDesign::getCreateTime);
        Page<BpmFormDesign> resultPage = bpmFormDesignService.page(page,queryWrapper);
        return R.ok(resultPage);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询表单设计分页")
    public R<List<BpmFormDesign>> getList(@RequestBody BpmFormDesign bpmFormDesign)
    {

        bpmFormDesign.setDelFlag(0);
        bpmFormDesign.setStatus(0);
        LambdaQueryWrapper<BpmFormDesign> queryWrapper = new LambdaQueryWrapper<>(bpmFormDesign);
        List<BpmFormDesign> list =  bpmFormDesignService.list(queryWrapper);
        return R.ok(list);
    }

    @ApiOperation(value = "获取表单定义类容")
    @GetMapping("/getForm/{id}")
    public R getForm(@PathVariable("id") Long id){
        BpmFormDesign bpmFormDesign = bpmFormDesignService.getById(id);
        return R.ok(bpmFormDesign);
    }

    @ApiOperation(value = "新增/修改 表单 表单编码、名称、内容不能为空")
    @PostMapping("/add")
    public R addForm(@Validated(Add.class) @RequestBody BpmFormDesign bpmFormDesign){
        return bpmFormDesignService.addForm(bpmFormDesign);
    }
    @ApiOperation(value = "删除表单 批量删除id 以逗号拼接")
    @DeleteMapping("/delete/{id}")
    public R deleteForm(@PathVariable("id") String id){
        return bpmFormDesignService.deleteForm(id);
    }
    @ApiOperation(value = "根据表单编码 查询表单版本详情记录")
    @PostMapping("/list/{code}")
    public R<List<BpmFormDesign>> selectFormList(@PathVariable("code") String code){
        LambdaQueryWrapper<BpmFormDesign> queryWrapper = new LambdaQueryWrapper<BpmFormDesign>().eq(BpmFormDesign::getCode,code).eq(BpmFormDesign::getDelFlag,0).orderByDesc(BpmFormDesign::getCreateTime);
        List<BpmFormDesign> list = bpmFormDesignService.list(queryWrapper);
        return R.ok(list);
    }

}
